package cn.sjxy.service;

import cn.sjxy.baseLib.SjuException;
import cn.sjxy.baseLib.SjuTool;
import cn.sjxy.mapper.Mp_BookInfo;
import cn.sjxy.mapper.Mp_Shopping;
import cn.sjxy.model.Md_BookInfo;
import cn.sjxy.model.Md_Shopping;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 购物车业务逻辑层
 */
@Service
public class Sv_Shopping {

    @Autowired
    private Mp_Shopping mp_shopping;

    @Autowired
    private Mp_BookInfo mp_bookInfo;

    /**
     * 添加商品到购物车
     * @param userCode
     * @param bookCode
     * @throws Exception
     */
    public void addShopping(String userCode, String bookCode) throws Exception {
        // 检查该用户购物车中是否已有该商品
        QueryWrapper<Md_Shopping> wrapper = new QueryWrapper<>();
        wrapper.eq("userCode", userCode);
        wrapper.eq("bookCode", bookCode);
        Md_Shopping existShopping = mp_shopping.selectOne(wrapper);

        if (existShopping != null) {
            // 已存在，数量+1
            existShopping.setBuyQty(existShopping.getBuyQty() + 1);
            mp_shopping.updateById(existShopping);
        } else {
            // 不存在，新增
            Md_Shopping newShopping = new Md_Shopping();
            newShopping.setUserCode(userCode);
            newShopping.setBookCode(bookCode);
            newShopping.setBuyQty(1);
            mp_shopping.insert(newShopping);
        }
    }

    /**
     * 增加购物车商品数量
     * @param userCode
     * @param bookCode
     * @throws Exception
     */
    public void addQty(String userCode, String bookCode) throws Exception {
        QueryWrapper<Md_Shopping> wrapper = new QueryWrapper<>();
        wrapper.eq("userCode", userCode);
        wrapper.eq("bookCode", bookCode);
        Md_Shopping shopping = mp_shopping.selectOne(wrapper);

        if (shopping == null) {
            SjuException.appEx("购物车中没有该商品");
        }

        shopping.setBuyQty(shopping.getBuyQty() + 1);
        mp_shopping.updateById(shopping);
    }

    /**
     * 减少购物车商品数量
     * @param userCode
     * @param bookCode
     * @throws Exception
     */
    public void subQty(String userCode, String bookCode) throws Exception {
        QueryWrapper<Md_Shopping> wrapper = new QueryWrapper<>();
        wrapper.eq("userCode", userCode);
        wrapper.eq("bookCode", bookCode);
        Md_Shopping shopping = mp_shopping.selectOne(wrapper);

        if (shopping == null) {
            SjuException.appEx("购物车中没有该商品");
        }

        if (shopping.getBuyQty() <= 1) {
            // 如果数量为1，则删除
            mp_shopping.deleteById(shopping.getId());
        } else {
            shopping.setBuyQty(shopping.getBuyQty() - 1);
            mp_shopping.updateById(shopping);
        }
    }

    /**
     * 删除购物车商品
     * @param userCode
     * @param bookCode
     * @throws Exception
     */
    public void deleteShopping(String userCode, String bookCode) throws Exception {
        QueryWrapper<Md_Shopping> wrapper = new QueryWrapper<>();
        wrapper.eq("userCode", userCode);
        wrapper.eq("bookCode", bookCode);
        mp_shopping.delete(wrapper);
    }

    /**
     * 获取用户购物车列表（带图书信息）
     * @param userCode
     * @return
     */
    public List<Md_Shopping> getShoppingList(String userCode) {
        QueryWrapper<Md_Shopping> wrapper = new QueryWrapper<>();
        wrapper.eq("userCode", userCode);
        List<Md_Shopping> shoppingList = mp_shopping.selectList(wrapper);

        // 填充图书信息
        for (Md_Shopping shopping : shoppingList) {
            QueryWrapper<Md_BookInfo> bookWrapper = new QueryWrapper<>();
            bookWrapper.eq("bookCode", shopping.getBookCode());
            Md_BookInfo bookInfo = mp_bookInfo.selectOne(bookWrapper);
            if (bookInfo != null) {
                shopping.setBookName(bookInfo.getBookName());
                shopping.setSellPrice(bookInfo.getSellPrice());
            }
        }

        return shoppingList;
    }

    /**
     * 清空指定用户的购物车（指定图书列表）
     * @param userCode
     * @param bookCodeList
     */
    public void clearShoppingByBookList(String userCode, List<String> bookCodeList) {
        QueryWrapper<Md_Shopping> wrapper = new QueryWrapper<>();
        wrapper.eq("userCode", userCode);
        wrapper.in("bookCode", bookCodeList);
        mp_shopping.delete(wrapper);
    }
}

